# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=ogre3d

pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.9.0
pkgrel=5
pkgdesc="A cross-platform 3D game engine (mingw-w64)"
arch=('any')
url="http://www.ogre3d.org/"
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config" "${MINGW_PACKAGE_PREFIX}-cmake" "mercurial")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
    "${MINGW_PACKAGE_PREFIX}-cppunit"
    "${MINGW_PACKAGE_PREFIX}-FreeImage"
    "${MINGW_PACKAGE_PREFIX}-freetype"
    "${MINGW_PACKAGE_PREFIX}-glsl-optimizer"
    "${MINGW_PACKAGE_PREFIX}-hlsl2glsl"
    "${MINGW_PACKAGE_PREFIX}-nvidia-cg-toolkit"
    "${MINGW_PACKAGE_PREFIX}-ois"
    "${MINGW_PACKAGE_PREFIX}-tinyxml"
    "${MINGW_PACKAGE_PREFIX}-winpthreads"
    "${MINGW_PACKAGE_PREFIX}-zlib"
    "${MINGW_PACKAGE_PREFIX}-zziplib"
    #"${MINGW_PACKAGE_PREFIX}-intel-tbb"
    )
options=('staticlibs' '!strip') # '!buildflags'
source=("${_realname}-${pkgver}.tar.xz"{,.sig}
    ogre-1.9.0-fix-cflags.patch
    ogre-endlessworld-fix.patch
    ogre-freeimage-fix.patch
    use-mingw-w64-directx.patch
    mingw-w64-fix.patch
    fix-using-locale.patch
)
md5sums=('7c128aa1bfb5f8793bc2f4c3a09edd0a'
         'SKIP'
         'a2cbfc846906bbb80c7b05d2d60a2779'
         '3c00c5584a8edcb4dc07a57697a73d59'
         '166486655cd358be175f3b32b63e0ca5'
         '07a29692ee7b30b90c8388af865078da'
         '56477180dc083746de83ea2aae3efd90'
         '591434cd5990dcad2ba21e5791c44029')

_buildtype=RelWithDebInfo

# source PKGBUILD && mksource
mksource() {
  [[ -x /usr/bin/hg ]] || (echo "hg not found. Install mercurial." && return 1)

  __hgroot='https://bitbucket.org/sinbad'
  __hgrepo='ogre'
  __hgbranch='default'
  __hgrev="v1-9"

  hg clone -u ${__hgrev} "${__hgroot}/${__hgrepo}/" ${__hgrepo}
  #hg clone -b ${__hgbranch} "${__hgroot}${__hgrepo}#${__hgrev}" ${__hgrepo}

  pushd ${__hgrepo}
  if (( $(hg id -n) < $(hg id -nr ${__hgbranch}) )); then
    printf 'You are not building the latest revision!\n'
    printf "Consider updating __hgrev to $(hg id -r ${__hgbranch}).\n"
  fi
  popd

  mv ${__hgrepo} ${_realname}-${pkgver}
  find ${_realname}-${pkgver} -depth -type d -name .hg -exec rm -rf {} \;
  rm ${_realname}-${pkgver}/{.hgignore,.hgtags}
  tar -cJf ${_realname}-${pkgver}.tar.xz ${_realname}-${pkgver}/*
  rm -r ${_realname}-${pkgver}

  gpg --detach-sign ${_realname}-${pkgver}.tar.xz

  #scp ${pkgname}-${pkgver}.tar.xz nym:/srv/ftp/other/vim/
  #scp ${pkgname}-${pkgver}.tar.xz.sig nym:/srv/ftp/other/vim/
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/ogre-1.9.0-fix-cflags.patch
  patch -p0 -i ${srcdir}/ogre-endlessworld-fix.patch
  patch -p1 -i ${srcdir}/ogre-freeimage-fix.patch
  patch -p1 -i ${srcdir}/use-mingw-w64-directx.patch
  patch -p1 -i ${srcdir}/mingw-w64-fix.patch
  patch -p1 -i ${srcdir}/fix-using-locale.patch
}

build() {
  mkdir -p ${pkgdir}${MINGW_PREFIX}
  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  export PREFIX_WIN=`pwd -W`
  popd > /dev/null

  pushd ${MINGW_PREFIX} > /dev/null
  export PREFIX_DEPS=`pwd -W`
  popd > /dev/null

  case ${MINGW_CHOST} in
    i686*)
      local _march=pentium4 #i686
      local _tune=generic #generic
    ;;
    x86_64*)
      local _march=core2
      local _tune=nocona
    ;;
  esac
  export CFLAGS="-O2 -pipe -march=${_march} -mtune=${_tune}"
  export CXXFLAGS="-O2 -pipe -march=${_march} -mtune=${_tune}"
  export DXSDK_DIR=${PREFIX_DEPS}/${MINGW_CHOST}
  mkdir ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}
    ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX:PATH=${PREFIX_WIN} \
    -DOGRE_DEPENDENCIES_DIR:PATH=${PREFIX_DEPS} \
    -DOGRE_INSTALL_TOOLS=ON \
    -DOGRE_COPY_DEPENDENCIES=OFF \
    -DOGRE_INSTALL_DEPENDENCIES=OFF \
    -DOGRE_BUILD_RENDERSYSTEM_D3D9=ON \
    -DOGRE_BUILD_RENDERSYSTEM_D3D11=OFF \
    -DCMAKE_BUILD_TYPE:STRING=${_buildtype} \
    -DFREETYPE_FT2BUILD_INCLUDE_DIR=${PREFIX_DEPS}/include/freetype2 \
    -DDirectX9_DXERR_LIBRARY=${PREFIX_DEPS}/${MINGW_CHOST}/lib/libdxerr9.a \
    ../${_realname}-${pkgver}

    make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make -j1 install #DESTDIR="$pkgdir"
  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null

  mv bin/${_buildtype}/* bin/
  rm -r bin/${_buildtype}
  mv lib/${_buildtype}/* lib/
  rm -r lib/${_buildtype}

  sed -i "s|../../|../|g" \
    bin/plugins.cfg \
    bin/resources.cfg \
    bin/quakemap.cfg

  mkdir -p share/cmake-2.8/Modules
  mv CMake/* share/cmake-2.8/Modules/
  rm -r CMake
  #rm -r media/CMakeFiles
  #rm media/cmake_install.cmake
  #rm media/Makefile

  #rm -r Docs/CMakeFiles
  #rm Docs/cmake_install.cmake
  #rm Docs/Makefile
  #rm Docs/CMakeLists.txt
  popd > /dev/null
}
